﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace Frogism.Data._2
{
    public abstract class RepositoryBase<T,I>
    {
        public void Save(T t)
        {
            using (NHibernate.ISession session = NHConfig.GetSessionFactory().OpenSession())
            {
                using (var trans = session.BeginTransaction())
                {
                    session.SaveOrUpdate(t);
                }
            }
        }
        public IEnumerable<T> GetAll()
        {
            using (NHibernate.ISession session = NHConfig.GetSessionFactory().OpenSession())
            {
                //return session.CreateCriteria(typeof(T)).List<T>();
                //return session.CreateQuery("from Book").List<T>();
                return session.CreateQuery("from " + typeof(T).Name).List<T>();
            }
        }
        public T Get(I id)
        {
            using (NHibernate.ISession session = NHConfig.GetSessionFactory().OpenSession())
            {
                return session.Get<T>(id);
            }
        }
    }
}
